package com.atguigu.rabbitmq.seven;

import com.atguigu.rabbitmq.utils.RabbitMqUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;

import java.nio.charset.StandardCharsets;

/**
 * ClassName: ReceiveLogsTopic02
 * Description: 声明主题交换机 及相关队列
 * 消费者 C1
 * Date: 2023/04/28 PM 3:08:37
 * <p>
 * project: atguigu-rabbitmq
 * package: com.atguigu.rabbitmq.seven
 * email: 1085844536@qq.com
 * version:
 *
 * @author WangGuojian
 */
public class ReceiveLogsTopic02 {

    /**
     * 交换机的名称
     */
    public static final String EXCHANGE_NAME = "topic_logs";

    /**
     * 接收消息
     */
    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        // 声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
        // 声明队列
        String queueName = "Q2";
        channel.queueDeclare(queueName, false, false, false, null);
        channel.queueBind(queueName, EXCHANGE_NAME, "*.*.rabbit");
        channel.queueBind(queueName, EXCHANGE_NAME, "lazy.#");
        System.out.println("等待接收消息......");

        // 接收消息
        channel.basicConsume(queueName, true, (consumerTag, message) -> {
            System.out.println(new String(message.getBody(), StandardCharsets.UTF_8));
            System.out.println("接受队列：" + queueName + "  绑定键：" + message.getEnvelope().getRoutingKey());
        }, consumerTag -> {
        });
    }
}
